package primary.primary0;

public class S0024两两交换链表中的节点 {


    /**
     * 答案，递归，其实就是接点转一下，然后返回当前新的头结点
     */
    class Solution2 {
        public ListNode swapPairs(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            ListNode newHead = head.next;
            head.next = swapPairs(newHead.next);
            newHead.next = head;
            return newHead;
        }
    }

    /**
     * 100
     * 我这个叫迭代?不清楚
     * 伪头结点，其他没啥
     */
    class Solution {
        public ListNode swapPairs(ListNode head) {
            ListNode fakeHead = new ListNode();
            fakeHead.next = head;
            swapHelp(fakeHead);
            return fakeHead.next;
        }
        private void swapHelp(ListNode node){
            if(node == null || node.next == null || node.next.next == null){
                return;
            }
            ListNode next1 = node.next;
            ListNode next2 = node.next.next;
            next1.next = next2.next;
            node.next = next2;
            next2.next = next1;
            swapHelp(node.next.next);
        }
    }
}
